package com.quickcancellation.crm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quickcancellation.crm.config.LogAspect;
import com.quickcancellation.crm.pojo.dto.PageDto;
import com.quickcancellation.crm.pojo.dto.PageParamsDto;
import com.quickcancellation.crm.pojo.dto.ResponseDto;
import com.quickcancellation.crm.pojo.entity.Giftrule;
import com.quickcancellation.crm.pojo.entity.Userinfo;
import com.quickcancellation.crm.service.IActivityService;
import com.quickcancellation.crm.service.IGiftruleService;
import com.quickcancellation.crm.service.IUserinfoService;
import com.quickcancellation.crm.utils.DateUtils;
import com.quickcancellation.crm.utils.PageUtils;
import com.quickcancellation.crm.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-03-07
 */

@RestController
@CrossOrigin
@RequestMapping("/takeRule")
@Api(tags = "搭赠规则")
@SaCheckLogin
@SaCheckRole("takeRule")
public class GiftruleController {

    @Resource
    private IActivityService activityService;

    @Resource
    private IUserinfoService userinfoService;

    @Resource
    private IGiftruleService giftruleService;
    @Resource
    HttpServletRequest request;

    private final static Logger logger = LoggerFactory.getLogger(GiftruleController.class);


    @PostMapping("/list")
    @ApiOperation("分页查询列表")
    @LogAspect
    public ResponseDto getActivityList(@RequestBody PageParamsDto<Map> params) {

        PageDto<Map> pageDto = PageUtils.getPageParams(params);


        List<Map> resultList = activityService.getGiftActivityList(pageDto);

        Long count = activityService.getGiftActivityListCount(pageDto);

        return ResponseUtil.success(count, resultList);
    }

    @PostMapping("/ruleList")
    @ApiOperation("分页查询列表")
    @LogAspect
    public ResponseDto getRuleList(@RequestBody Map params) {

        Long activityId = MapUtils.getLong(params, "activityId");

        QueryWrapper<Giftrule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activityId",activityId);
        queryWrapper.orderByAsc("standard");

        List<Giftrule> resultList = giftruleService.list(queryWrapper);

        return ResponseUtil.success(resultList);

    }

    @PostMapping("/save")
    @ApiOperation("保存规则")
    @LogAspect
    public ResponseDto getActivityList(@RequestBody Giftrule giftrule) {

        giftrule.setRuleGuid(UUID.randomUUID().toString());

        Userinfo userinfo = userinfoService.getUserInfoByRequest(request);

        String userId = userinfo.getId();

//        giftrule.setCreateOp(Long.valueOf(userId));

        giftrule.setCreateTime(DateUtils.getNow());


        boolean result = giftruleService.save(giftrule);

        return ResponseUtil.ok(result);

    }

    @PostMapping("/delete")
    @ApiOperation("删除规则")
    @LogAspect
    public ResponseDto delete(@RequestBody Giftrule giftrule) {

        Long id = giftrule.getId();

        boolean result = giftruleService.removeById(id);

        return ResponseUtil.ok(result);

    }

}
